package db

import (
	"context"
	pb "gitee.com/airport_container/grpc_interface/gosdk/job"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo/options"
)

// CreateJob 创建作业
func (s *MgoStore) CreateJob(model *pb.JobModel) error {
	collection := s.Client.Database(DbName).Collection("job")
	_, err := collection.InsertOne(context.Background(), model)
	return err
}

func (s *MgoStore) GetLastJob() (*pb.JobModel, error) {
	collection := s.Client.Database(DbName).Collection("job")
	opts := options.FindOne().SetSort(bson.D{{"_id", -1}}) // 按照_id字段降序排序

	var job pb.JobModel
	err := collection.FindOne(context.Background(), bson.M{}, opts).Decode(&job)
	return &job, err
}
